<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\App;

class SummaryUser extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\SummaryUser';

    //关联模型
    protected array $hasOne = ['user'];

    //自定义方法排序
    protected array $sort = ['id' => 'desc'];

    public function index(): string
    {
        $table = Builder::Table()
            ->addColumn(['field' => 'uid', 'title' => '相关会员', 'templet' => '#_userTpl', 'width' => 250]);
        foreach (getBonusTitle(2) as $k => $v) {
            $table->addColumn(['field' => $k, 'templet' => '<span>{{d.' . $k . '}}</span>', 'title' => $v, 'totalRow' => true]);
        }
        $table->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['export'])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']
            ])
            ->addRightButtons([
                'summary_info' => [
                    'title' => '查看明细',
                    'type' => 'summary_info',
                    'url' => url('summary_info')->build()
                ]
            ])
            ->setTotalRaw('true')
            ->addJsUrl(['summary']);
        return $table->fetch();
    }

    public function summary_info()
    {
        $id = $this->request->param('id');
        $info = $this->model::find($id);
        $create_time = $info->create_date . ' - ' . $info->create_date;
        $url = url('account/bonus') . '?create_time=' . $create_time . '&uid=' . $info->user->code;

        return redirect($url);
    }
}